<template>
    <view class="manage_info_components contract_region">
        <div class="pub_input">
            <div class="li">
                <div class="title">所属区域</div>
                <div class="input">
                    <input class="inp" type="text" :value="areaName" placeholder="请选择" placeholder-style="color:#bbb"
                        disabled>
                </div>
            </div>
            <div class="li">
                <div class="title"><text class="xing">*</text> 房间名称</div>
                <div class="input">
                    <input class="inp" type="text" v-model="form.roomName" placeholder="请输入" placeholder-style="color:#bbb">
                </div>
            </div>
        </div>
        <div class="pub_btns" v-if="editId == ''" :style="{ 'bottom': bottom }">
            <div class="pub_btn pub_btn_big_normal" @click="toSubmit">保存并提交</div>
        </div>
        <div class="pub_btns" v-else :style="{ 'bottom': bottom }">
            <div class="pub_btn_normal pub_btn_normal_border pub_btn_small" @click="toDelete">删除</div>
            <div class="pub_btn pub_btn_small" @click="toSubmit">保存并提交</div>
        </div>
    </view>
</template>

<script>
export default {
    props: {
        editId: {
            type: String,
            default: ''
        },
        areaId: {
            type: String,
            default: ''
        },
        areaName: {
            type: String,
            default: ''
        },
    },
    data() {
        return {
            bottom: `calc(${getApp().globalData.statusBottomHeight}px + 32rpx)`,
            form: {
                roomName: ''
            }
        };
    },
    watch: {
        editId: {
            handler() {
                if (this.editId) this.getDetail()
            },
            immediate: true,
            deep: true
        },
    },
    onLoad() { },
    methods: {
        async getDetail() {
            const that = this;
            that.$comFun.openLoading();
            const res = await that.$request.get(`/mobile/manager/room/${that.editId}`, {});
            // console.log(res);
            that.$comFun.closeLoading();
            if (res.code == 200) {
                this.form.roomName = res.data.roomName;
            }
        },
        async toSubmit() {
            const that = this;
            if (!that.form.roomName) {
                that.$comFun.toast('请填写房间名称');
                return;
            }
            uni.showModal({
                title: '提示',
                content: '是否确定保存？',
                success: async function (modalRes) {
                    if (modalRes.confirm) {
                        that.$comFun.openLoading();
                        let res;
                        if (that.editId) {
                            res = await that.$request.put('mobile/manager/room', {
                                ...that.form,
                                roomId: Number(that.editId)
                            });
                        } else {
                            res = await that.$request.post('mobile/manager/room', {
                                ...that.form,
                                areaId: Number(that.areaId)
                            });
                        }
                        // console.log(res);
                        that.$comFun.closeLoading();
                        if (res.code == 200) {
                            that.$comFun.toast('保存成功');
                            uni.$emit(`manageListReload_4`);
                            uni.$emit(`manageListReload_5`);
                            setTimeout(() => {
                                that.$comFun.closeWin();
                            }, 1000)
                        } else {
                            that.$comFun.toast(res.msg);
                        }
                    }
                }
            });
        },
        toDelete() {
            const that = this;
            uni.showModal({
                title: '提示',
                content: '是否确定删除？',
                success: async function (modalRes) {
                    if (modalRes.confirm) {
                        that.$comFun.openLoading();
                        const res = await that.$request.delete(`mobile/manager/room/${that.editId}`);
                        // console.log(res);
                        that.$comFun.closeLoading();
                        if (res.code == 200) {
                            that.$comFun.toast('删除成功');
                            uni.$emit(`manageListReload_4`);
                            uni.$emit(`manageListReload_5`);
                            setTimeout(() => {
                                that.$comFun.closeWin();
                            }, 1000)
                        } else {
                            that.$comFun.toast(res.msg);
                        }
                    }
                }
            });
        },
    },
};
</script>

<style lang="scss" scoped>
@import "@/common/css/manage.scss";
</style>
